<template>
  <div class="login">
    <div class="mid-box">
      <div class="left-content transerve">
        <h1>欢迎来到虚拟宠物医院</h1>
        <h3>Welcome to Your Virtual Pet Hospital</h3>
      </div>
      <el-form
        class="right-content transerve"
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        :hide-required-asterisk=true>
        <h3 class="title">管理员登录</h3>
        <el-form-item prop="email" label="账号：">
          <el-input
            v-model="loginForm.email"
            type="text"
            auto-complete="off"
            placeholder="账号"
            id="input">
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码：">
          <el-input
            v-model="loginForm.password"
            type="password"
            show-password
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin">
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled" label="验证码：">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin">
          </el-input>
          <div class="login-code" width="100%" @click="refreshCode">
            <dentify :identifyCode="identifyCode"></dentify>
          </div>
        </el-form-item>
        <el-form-item style="width: 100%; text-align: center">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 85%"
            @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
        <!-- <div style="float: left; margin-top: 10px" v-if="register">
          <router-link class="link-type" :to="'/backlogin'"
            >我是管理员</router-link
          >
        </div> -->
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer"></div>
  </div>
</template>
  <script>
  import {backLogIn} from '../../api/index'
  import dentify from "../yanzhengma.vue";
  export default {
    name: 'login',
    components: {
      dentify,
    },
    data() {
      return {
        codeUrl: '',
        loginForm: {
          email: '',
          password: '',
          rememberMe: false,
          code: '',
          uuid: '',
        },
        loginRules: {
          email: [
            {
              required: true,
              message: '请输入邮箱',
              trigger: 'blur',
            },
            {
              validator: function (rule, value, callback) {
                if (
                  /^\w{1,64}@[a-z0-9\-]{1,256}(\.[a-z]{2,6}){1,5}$/i.test(
                    value
                  ) == false
                ) {
                  callback(new Error('邮箱格式错误'))
                } else {
                  callback()
                }
              },
              trigger: 'blur',
            },
          ],
          password: [
            { required: true, trigger: 'blur', message: '请输入您的密码' },
          ],
          // code: [{ required: true, trigger: 'change', message: '请输入验证码' }],
        },
        loading: false,
        // 验证码开关
        captchaEnabled: true,
        // 注册开关
        register: true,
        redirect: undefined,
        identifyCodes: "1234567890abcdefjhijklinopqrsduvwxyz", //随机串内容,从这里随机抽几个显示验证码
        identifyCode: "", //验证码图片内容
      }
    },
    mounted(){
      // window.getSession = this.getSession
      sessionStorage.setItem('sessionId', '');
      this.identifyCode = "";
      //参数：（1）随机串内容。（2）验证码显示位数
      this.makeCode(this.identifyCodes, 4);
    },
    methods: {
      // 重置验证码
      refreshCode() {
        this.identifyCode = "";
        this.makeCode(this.identifyCodes, 4);
      },
      //获取验证码的值
      makeCode(o, l) {
        for (let i = 0; i < l; i++) {
          //通过循环获取字符串内随机几位
          this.identifyCode +=
            this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
        }
      },
      //随机数字：用于当角标拿字符串的值
      randomNum(min, max) {
        return Math.floor(Math.random() * (max - min) + min);
      },
  
      // getCode(){
      //   this.$message.success("验证码")
      // },
      handleLogin() {
        this.$refs.loginForm.validate((valid) => {
        //登录接口
          if(valid){
            if(this.loginForm.code == this.identifyCode){  
            let dataLogin = {}
            dataLogin.email = this.loginForm.email
            dataLogin.password = this.loginForm.password
            backLogIn(dataLogin).then(res => {
              if(res.data.code == 200){
                console.log('access_token',res)
                sessionStorage.setItem('access_token',res.data.data.access_token)
                sessionStorage.setItem('admin',res.data.data.user_id)
                this.$router.push("/user").catch(err => { console.log(err) })
              }
              else{
                this.$message.error(res.data.message)
              }            
            })
          }
          else{
            this.$message.error("验证码错误")
          }
        }
        
      })
      },
      //传递sessionId的函数
      // getSession(){
      //   return this.sessionId
      // }
    },
  }
  </script>
  
  <style lang="scss" scoped>
  .login {
    display: flex;
    // justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url('../../assets/images/bg.png');
    background-size: 100% 100%;
  }
  .title {
    margin: 0px auto 50px auto;
    text-align: center;
    color: #707070;
    font-weight: 600;
    font-size: 27px;
  }
  
  .login-form {
    border-radius: 6px;
    // background: #ffffff;
    background-color: rgba(255, 255, 255, 0.7);
    // width: 400px;
    width: 600px;
    // padding: 25px 25px 5px 25px;
    padding: 50px 100px 70px 100px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    height: 38px;
    float: right;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
  .login-code-img {
    height: 38px;
  }
  .mid-box {
  margin-top: 20px;
  width: 58%;
  height: 70%;
  margin-left: 20px;
  border-radius: 25px;
  padding: 30px 20px;
  display: flex;
  flex-direction: row;
  background-color: rgba(255, 255, 255, 0.4);
}
.transerve {
  flex: 1;
}
.left-content {
  margin-top: 10px;
  text-align: center;
  padding-top: 150px;
  height: 80%;
}
.right-content {
  margin-top: 10px;
  height: 90%;
  background-color: rgba(255, 255, 255, 0.5);
  border-radius: 25px;
  padding: 30px 30px;
  box-sizing: border-box;
}
.itemflex {
  display: flex;
  flex-direction: row;
}
::v-deep .el-form-item__content {
  display: inline-block;
  width: 80%;
}
::v-deep .el-form-item__label {
  width: 70px;
}
  </style></style>
  